package com.hdu.hot100.binarySearch;

public class lc34 {
    class Solution {
        public int[] searchRange(int[] nums, int target) {
            return new int[]{find(nums, true, target), find(nums, false, target)};
        }

        int find(int[] nums, boolean findFirst, int target) {
            int ans = -1;
            int l = 0;
            int r = nums.length - 1;
            while (l <= r) {
                int mid = l + (r - l) / 2;
                if (nums[mid] == target) {
                    ans = mid;
                    if (findFirst) {
                        r = mid - 1;
                    } else {
                        l = mid + 1;
                    }
                } else if (nums[mid] > target) {
                    r = mid - 1;
                } else {
                    l = mid + 1;
                }
            }
            return ans;
        }
    }
}
